package com.sip.stream.utils.zfy.muxer;

import android.media.MediaFormat;
import android.util.Log;

import com.sip.stream.media.ZFYMediaManager;
import com.sip.stream.utils.zfy.GSMediaSampleData;
import com.sip.stream.utils.zfy.ZFYFilePath;
import com.sip.stream.utils.zfy.consumer.IGSAudioEncoderConsumer;
import com.sip.stream.utils.zfy.consumer.IGSMediaObserver;
import com.sip.stream.utils.zfy.consumer.IGSVideoEncoderConsumer;
import com.sip.stream.utils.zfy.encoder.IGSAudioEncoder;
import com.sip.stream.utils.zfy.encoder.IGSVideoEncoder;

import java.io.File;
import java.util.Date;

public class ZFYPreRecordConsumer implements IGSMediaMuxer {
    private static final String TAG = "ZFYPreRecordConsumer";
    private IGSAudioEncoder audioEncoder;
    private IGSMediaObserver<IGSAudioEncoderConsumer> audioEncoderConsumerObserver;
    private IGSAudioEncoderConsumer audioEncoderLisener;
    private MediaFormat audioOutputFormat;
    private GSBareStreamWriter bareStreamWriter;
    private long lastTimeUS;
    private Date startDate;
    private IGSVideoEncoder videoEncoder;
    private IGSMediaObserver<IGSVideoEncoderConsumer> videoEncoderConsumerObserver;
    private IGSVideoEncoderConsumer videoEncoderLisener;
    private MediaFormat videoOutputFormat;
    private String lastFilePath = null;
    private String currentFilePath = null;
    private long startPts = 0;

    @Override
    public void forceSwitchMuxer() {
    }

    @Override
    public void start() {
    }

    public ZFYPreRecordConsumer(IGSVideoEncoder iGSVideoEncoder, IGSAudioEncoder iGSAudioEncoder) {
        this.videoEncoder = iGSVideoEncoder;
        this.audioEncoder = iGSAudioEncoder;
        cleanUpFiles();
        this.lastTimeUS = ZFYMediaManager.getInstance().getConfig().getVideoPreTime() * 1000000;
        Log.i(TAG, "lastTimeUS" + this.lastTimeUS);
        this.videoEncoder.setForcedInterval(1);
        setUpListener(this.videoEncoder, this.audioEncoder);
        this.startDate = new Date();
    }

    private boolean cleanUpFiles() {
        File[] listFiles = new File(ZFYFilePath.getPreRecordingCacheDirPath()).listFiles();
        if (listFiles != null) {
            for (File file : listFiles) {
                file.delete();
            }
            return true;
        }
        return true;
    }

    private void setUpListener(IGSVideoEncoder iGSVideoEncoder, IGSAudioEncoder iGSAudioEncoder) {
        IGSAudioEncoderConsumer iGSAudioEncoderConsumer = new IGSAudioEncoderConsumer() {
            @Override
            public void encodeAudioError(String str) {
            }

            @Override
            public void encodeAudioSuccess(GSMediaSampleData gSMediaSampleData) {
                ZFYPreRecordConsumer.this.writeSampleData(gSMediaSampleData);
            }

            @Override
            public void setOutputFormat(MediaFormat mediaFormat) {
                ZFYPreRecordConsumer.this.setAudioOutputFormat(mediaFormat);
            }

            @Override
            public void addObserver(IGSMediaObserver<IGSAudioEncoderConsumer> iGSMediaObserver) {
                ZFYPreRecordConsumer.this.audioEncoderConsumerObserver = iGSMediaObserver;
            }
        };
        this.audioEncoderLisener = iGSAudioEncoderConsumer;
        iGSAudioEncoder.addLisener(iGSAudioEncoderConsumer);
        IGSVideoEncoderConsumer iGSVideoEncoderConsumer = new IGSVideoEncoderConsumer() {
            @Override
            public void encodeVideoError(String str) {
            }

            @Override
            public void encodeVideoSuccess(GSMediaSampleData gSMediaSampleData) {
                ZFYPreRecordConsumer.this.writeSampleData(gSMediaSampleData);
            }

            @Override
            public void setOutputFormat(MediaFormat mediaFormat) {
                ZFYPreRecordConsumer.this.setVideoOutputFormat(mediaFormat);
            }

            @Override
            public void addObserver(IGSMediaObserver<IGSVideoEncoderConsumer> iGSMediaObserver) {
                ZFYPreRecordConsumer.this.videoEncoderConsumerObserver = iGSMediaObserver;
            }
        };
        this.videoEncoderLisener = iGSVideoEncoderConsumer;
        iGSVideoEncoder.addLisener(iGSVideoEncoderConsumer);
    }

    @Override
    public void stop() {
        Log.i(TAG, "stop");
        stopAction();
        release();
    }

    public void stopAction() {
        GSBareStreamWriter gSBareStreamWriter = this.bareStreamWriter;
        if (gSBareStreamWriter != null) {
            gSBareStreamWriter.stop();
        }
    }

    public void release() {
        Log.i(TAG, "release");
        IGSMediaObserver<IGSAudioEncoderConsumer> iGSMediaObserver = this.audioEncoderConsumerObserver;
        if (iGSMediaObserver != null) {
            iGSMediaObserver.onStop(this.audioEncoderLisener);
            this.audioEncoderLisener = null;
        }
        IGSMediaObserver<IGSVideoEncoderConsumer> iGSMediaObserver2 = this.videoEncoderConsumerObserver;
        if (iGSMediaObserver2 != null) {
            iGSMediaObserver2.onStop(this.videoEncoderLisener);
            this.videoEncoderLisener = null;
        }
    }

    public void setVideoOutputFormat(MediaFormat mediaFormat) {
        this.videoOutputFormat = mediaFormat;
    }

    public void setAudioOutputFormat(MediaFormat mediaFormat) {
        this.audioOutputFormat = mediaFormat;
    }

    public synchronized void writeSampleData(GSMediaSampleData gSMediaSampleData) {
        if (gSMediaSampleData.getPresentationTimeUs() <= 0) {
            return;
        }
        getWriter(gSMediaSampleData.getPresentationTimeUs()).writeSampleData(gSMediaSampleData);
    }

    private GSBareStreamWriter getWriter(long j) {
        GSBareStreamWriter gSBareStreamWriter = this.bareStreamWriter;
        if (gSBareStreamWriter == null) {
            this.startPts = j;
            String str = ZFYFilePath.getPreRecordingCacheDirPath() + j;
            this.currentFilePath = str;
            GSBareStreamWriter gSBareStreamWriter2 = new GSBareStreamWriter(str);
            this.bareStreamWriter = gSBareStreamWriter2;
            gSBareStreamWriter2.start();
            Log.w(TAG, "init getWriter " + this.startPts);
        } else if (j - this.startPts > this.lastTimeUS) {
            gSBareStreamWriter.stop();
            this.startPts = j;
            GSBareStreamWriter gSBareStreamWriter3 = new GSBareStreamWriter(ZFYFilePath.getPreRecordingCacheDirPath() + j);
            this.bareStreamWriter = gSBareStreamWriter3;
            gSBareStreamWriter3.start();
            switchFile(ZFYFilePath.getPreRecordingCacheDirPath() + j);
            Log.w(TAG, "next getWriter:" + j);
        }
        return this.bareStreamWriter;
    }

    private void switchFile(String filePath) {
        String str2 = this.lastFilePath;
        if (str2 != null && str2.isEmpty()) {
            File file = new File(this.lastFilePath);
            if (file.exists()) {
                file.delete();
            }
        }
        this.lastFilePath = this.currentFilePath;
        this.currentFilePath = filePath;
    }

    public MediaFormat getVideoOutputFormat() {
        return this.videoOutputFormat;
    }

    public MediaFormat getAudioOutputFormat() {
        return this.audioOutputFormat;
    }

    public IGSVideoEncoder getVideoEncoder() {
        return this.videoEncoder;
    }

    public IGSAudioEncoder getAudioEncoder() {
        return this.audioEncoder;
    }

    public String getLastFilePath() {
        return this.lastFilePath;
    }

    public String getCurrentFilePath() {
        return this.currentFilePath;
    }

    public long getLastTimeUS() {
        return this.lastTimeUS;
    }

    public Date getStartDate() {
        return this.startDate;
    }
}
